%% Author: vivimice
%% Created: Jan 10, 2012
%% Description: array based map, linear stored
-module(map).

%%
%% Include files
%%

%%
%% Exported Functions
%%
-export([create/0, get/2, contains/2, put/3, remove/2, keys/1]).

%%
%% API Functions
%%
create() ->
	[].

get(Map, Key) ->
	FoundItems = [Item || Item <- Map, test_item(Item, Key)],
	if
		length(FoundItems) == 1 ->
			[{Key, Value}] = FoundItems,
			Value;
		
		true -> 
			null
	end.

contains(Map, Key) ->
	length([Item || Item <- Map, test_item(Item, Key)]) > 0.

put(Map, Key, Value) ->
	?MODULE:remove(Map, Key) ++ [{Key, Value}].

remove(Map, Key) ->
	[Item || Item <- Map, test_item(Item, Key) /= true].

keys(Map) ->
	[get_key(Item) || Item <- Map].

%%
%% Local Functions
%%
test_item({TestKey, _TestValue}, Key) ->
	TestKey =:= Key.

get_key({Key, _Value}) ->
	Key.